package com.ruoyi.common.core.constant;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;

/**
 * 任务调度通用常量
 *
 * @author RuoYi
 */
public class ScheduleConstants {
    public static final String TASK_CLASS_NAME = "TASK_CLASS_NAME";

    /**
     * 执行目标key
     */
    public static final String TASK_PROPERTIES = "TASK_PROPERTIES";
    /**
     * 解析出 {@link #TASK_PROPERTIES} 中的数据到 {@link #TASK_PARAMS}
     */
    public static final String TASK_PARAMS = "TASK_PARAMS";
    public static final String TASK_DEPENDENT = "TASK_DEPENDENT";
    public static final String TASK_RUN_MODE = "TASK_RUN_MODE";
    public static final String TASK_DEPENDENT_ID = "TASK_DEPENDENT_ID";

    /**
     * 默认
     */
    public static final String MISFIRE_DEFAULT = "0";

    /**
     * 立即触发执行
     */
    public static final String MISFIRE_IGNORE_MISFIRES = "1";

    /**
     * 触发一次执行
     */
    public static final String MISFIRE_FIRE_AND_PROCEED = "2";

    /**
     * 不触发立即执行
     */
    public static final String MISFIRE_DO_NOTHING = "3";
    /* 执行器名称 start */
    public static final String BEAN = "bean";
    public static final String PROCEDURE = "procedure";
    public static final String PYTHON = "python";
    public static final String SHELL = "shell";
    /* 执行器名称 end */
    /**
     * 日期相减天数
     */
    public static final String DATE_SUBTRACT_DAY = "dateSubtractDay";
    /**
     * 立即执行
     */
    public static String EXECUTE_IMMEDIATELY = "EXECUTE_IMMEDIATELY";
    /**
     * 定时执行
     */
    public static String TIMED_EXECUTION = "TIMED_EXECUTION";
    /**
     * 异常执行
     */
    public static String EXCEPTION_EXECUTION = "EXCEPTION_EXECUTION";

    public static final List<String> BATCH_TYPE_DEFAULTS = Arrays.asList("SECOND", "MINUTE", "HOUR");

    @Getter
    @AllArgsConstructor
    public enum Status {
        /**
         * 正常
         */
        NORMAL("0"),
        /**
         * 暂停
         */
        PAUSE("1");

        private final String value;
    }

    @Getter
    @AllArgsConstructor
    public enum BatchType {
        /**
         * 秒任务
         */
        SECOND("SECOND", "yyyyMMddHHmmss"),
        /**
         * 分钟任务
         */
        MINUTE("MINUTE", "yyyyMMddHHmm"),
        /**
         * 小时
         */
        HOUR("HOUR", "yyyyMMddHH"),
        /**
         * 天
         */
        DAY("DAY", "yyyyMMdd"),
        /**
         * 月
         */
        MONTH("MONTH", "yyyyMM"),
        /**
         * 年
         */
        YEAR("YEAR", "yyyy");

        private final String key;
        private final String value;

        public static String getBatchTypeKey(String key) {
            for (BatchType batchType : BatchType.values()) {
                if (batchType.getKey().equals(key)) {
                    return batchType.getKey();
                }
            }
            return null;
        }

        public static String getBatchTypeValue(String key) {
            for (BatchType batchType : BatchType.values()) {
                if (batchType.getKey().equals(key)) {
                    return batchType.getValue();
                }
            }
            return null;
        }
    }
}
